package cn.com.zal.cgweb.common.exception;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;

public class NoLoginExceptionHandle extends ExceptionHandler {
	public ActionForward execute(Exception ex, ExceptionConfig config,
			ActionMapping mapping, ActionForm formInstance,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
		ActionForward forward = null;
		ActionError error = null;
		String property = null;
		String path = null;
		path = config.getPath() != null ? config.getPath() : mapping.getInput();
		forward = new ActionForward(path);
		
		//error = this.getDiyError("error.DIYException", ex);
		//ex.printStackTrace();
		//property = error.getKey();
		//storeException(request, property, error, forward, config.getScope());
		request.setAttribute("noLoginException", ex);
		
		return new ActionForward("/error/noLogin.jsp");
	}

	private ActionError getDiyError(String key, Throwable t) {
		StringBuffer buf = new StringBuffer();
		buf.append(t.getMessage());
		buf.append("\n\n");
		buf.append("<!--");
		for (int i = 0; i < t.getStackTrace().length; i++) {
			buf.append(t.getStackTrace()[i].toString()).append("\n");
		}
		buf.append("-->");
		return new ActionError(key, t.getMessage(), buf.toString());
	}
}
